package com.study.config;

import org.csource.common.MyException;
import org.csource.fastdfs.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Properties;

@Component
public class FastDFSConfigInitializer  implements CommandLineRunner {

    @Autowired
    private FastDFSConfig fastDFSConfig;

    @Override
    public void run(String... args) throws Exception {
        initFastDFSConfig();
    }

    private void initFastDFSConfig() {
        try {
            Properties properties = new Properties();
            properties.setProperty("fastdfs.tracker_servers", fastDFSConfig.getTrackerServer());
            properties.setProperty("fastdfs.connect_timeout_in_seconds", String.valueOf(fastDFSConfig.getConnectTimeout()));
            properties.setProperty("fastdfs.network_timeout_in_seconds", String.valueOf(fastDFSConfig.getNetworkTimeout()));
            /**
             * fastdfs.tracker_servers：配置 Tracker 服务器的地址列表，格式为 ip:port
             * fastdfs.connect_timeout_in_seconds：连接超时时间，单位为秒
             * fastdfs.charset：字符集编码，默认为 UTF-8
             * fastdfs.http.tracker_http_port：HTTP Tracker 服务器端口，默认为 80
             * fastdfs.http.anti_steal_token：是否启用防盗链，默认为 false
             * fastdfs.http.secret_key：防盗链密钥，用于生成防盗链
             *
             * fastdfs.http.access_token：是否启用访问令牌，默认为 false
             * fastdfs.http.upload_file_length：上传文件长度，默认为 0
             * fastdfs.http.file_pool_size：文件池大小，默认为 10
             * fastdfs.http.network_timeout_in_seconds：HTTP 网络超时时间，默认为 30
             * fastdfs.http.charset：HTTP 字符集编码，默认为 UTF-8
             * fastdfs.http.anti_steal_token_secret_key：防盗链密钥，用于生成防盗链
             * fastdfs.http.access_token_secret_key：访问令牌密钥，用于生成访问令牌
             * fastdfs.http.anti_steal_token_expire_seconds：防盗链过期时间，默认为 3600
             * fastdfs.http.access_token_expire_seconds：访问令牌过期时间，默认为 3600
             * fastdfs.http.anti_steal_token_header_name：防盗链请求头名称，默认为 "anti-steal-token"
             * fastdfs.http.access_token_header_name：访问令牌请求头名称，默认为 "access-token"
             * fastdfs.http.anti_steal_token_param_name：防盗链请求参数名称，默认为 "token"
             */
            ClientGlobal.initByProperties(properties);
        } catch (IOException | MyException e) {
            throw new RuntimeException("Failed to initialize FastDFS Client", e);
        }
    }

    /**
     * 获取Tracker服务地址
     *
     * @return Tracker服务地址
     * @throws IOException 获取失败时抛出异常
     */
    public String getTrackerUrl() throws IOException {
        return "http://" + getTrackerServer().getInetSocketAddress().getHostString() + ":" + ClientGlobal.getG_tracker_http_port() + "/";
    }

    /**
     * 获取Storage组
     *
     * @return StorageServer 数组
     * @throws IOException 获取失败时抛出异常
     */
    public StorageServer[] getStoreStorages() throws IOException {
        TrackerClient trackerClient = new TrackerClient();
        TrackerServer trackerServer = trackerClient.getConnection();
        return trackerClient.getStoreStorages(trackerServer, fastDFSConfig.getGroupName());
    }

    /**
     * 获取Storage信息,IP和端口
     * @param remoteFileName 文件存储完整名
     * @return ServerInfo 数组
     * @throws IOException 获取失败时抛出异常
     */
    public ServerInfo[] getFetchStorages(String remoteFileName) throws IOException {
        TrackerClient trackerClient = new TrackerClient();
        TrackerServer trackerServer = trackerClient.getConnection();
        return trackerClient.getFetchStorages(trackerServer, fastDFSConfig.getGroupName(), remoteFileName);
    }



    /**
     * 获取Storage客户端
     *
     * @return StorageClient 对象
     * @throws IOException 获取失败时抛出异常
     */
    public StorageClient getStorageClient() throws IOException {
        TrackerServer trackerServer = getTrackerServer();
        return new StorageClient(trackerServer, null);
    }

    /**
     * 获取Tracker
     *
     * @return TrackerServer 对象
     * @throws IOException 获取失败时抛出异常
     */
    public TrackerServer getTrackerServer() throws IOException {
        TrackerClient trackerClient = new TrackerClient();
        return trackerClient.getConnection();
    }

}
